#include "timeutility.h"


#include <QDateTime>


namespace Utility {

TimeUtility::TimeUtility(QObject *parent) :
    QObject(parent)
{
}


qint64 TimeUtility::lastWeekSecsSinceEpoch()
{
    QDateTime lastWeekDateTime = QDateTime::currentDateTime().addDays(-7);
    return lastWeekDateTime.toMSecsSinceEpoch()/1000;
}


qint64 TimeUtility::lastMonthSecsSinceEpoch()
{
    QDateTime lastMonthDateTime = QDateTime::currentDateTime().addMonths(-1);
    return lastMonthDateTime.toMSecsSinceEpoch()/1000;
}


qint64 TimeUtility::lastYearSecsSinceEpoch()
{
    QDateTime lastYearDateTime = QDateTime::currentDateTime().addYears(-1);
    return lastYearDateTime.toMSecsSinceEpoch()/1000;
}


qint64 TimeUtility::secsTimePeriodDay()
{
    return 24*60*60;
}


qint64 TimeUtility::msecsTimePeriodDay()
{
    return 24*60*60*1000;
}


qint64 TimeUtility::beginningTodayMSecsFromEpoch()
{
    qint64 msecsTimePeriodDay = 24*60*60*1000;
    qint64 currentMSecsFromEpoch = QDateTime::currentMSecsSinceEpoch();
    return currentMSecsFromEpoch - (currentMSecsFromEpoch % msecsTimePeriodDay);
}


qint64 TimeUtility::beginningThisWeekMSecsFromEpoch()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QDate currentDate = currentDateTime.date();
    int dayOfWeek = currentDate.dayOfWeek();
    qint64 currentMSecsFromEpoch = QDateTime::currentMSecsSinceEpoch();
    return currentMSecsFromEpoch - TimeUtility::beginningTodayMSecsFromEpoch() - \
            (dayOfWeek - 1)*(TimeUtility::msecsTimePeriodDay());
}


qint64 TimeUtility::beginningThisWeekSecsFromEpoch()
{
    return TimeUtility::beginningThisWeekMSecsFromEpoch()/1000;
}


bool TimeUtility::isToday(int secsTime)
{
    qint64 beginningTodayMSecsFromEpoch = TimeUtility::beginningTodayMSecsFromEpoch();
    qint64 beginningTodaySecsFromEpoch = beginningTodayMSecsFromEpoch/1000;
    //qint64 currentSecsFromEpoch = QDateTime::currentMSecsSinceEpoch()/1000;
    if (secsTime > beginningTodaySecsFromEpoch)
        return true;

    return false;
}


bool TimeUtility::isThisWeek(int secsTime)
{
    qint64 currentSecsFromEpoch = QDateTime::currentMSecsSinceEpoch()/1000;
    qint64 beginningThisWeekSecsFromEpoch = TimeUtility::beginningThisWeekSecsFromEpoch();
    if ((secsTime <= currentSecsFromEpoch) && (secsTime > beginningThisWeekSecsFromEpoch))
        return true;

    return false;
}

} // namespace Utility
